package com.finance.framework.qurtz;

import com.finance.framework.lang.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列
 * 为每一个单独的任务触发器维护一个队列
 * 队列内容为[pageNo:1,pageSize:200],[pageNo:2,pageSize:200]
 */
@Component
@Scope("prototype")
public class PageBlockingQueue implements Serializable{
    private Logger logger = LoggerFactory.getLogger(PageBlockingQueue.class);

    private BlockingQueue<Page> queue = new LinkedBlockingQueue<Page>(10);

    public PageBlockingQueue(){
        this.logger.info("PageBlockingQueue created!");
    }
    @Deprecated
    public Page get() throws InterruptedException {
        Page page = queue.take();
        this.logger.info("PageBlockingQueue get: " + page.toString());
        return page;
    }
    public Page get(TimeUnit timeUnit,long time) throws InterruptedException {
        Page page = null;
        page = queue.poll(time,timeUnit);
        this.logger.info("PageBlockingQueue get: " + String.valueOf(page));
        return page;
    }
    public void put(Page page){
        this.queue.offer(page);
        this.logger.info("PageBlockingQueue put: " + page.toString());
    }
    @PostConstruct
    public void init(){
        this.logger.info("PageBlockingQueue init");
        int perSize = 200;
        for(int i = 0; i <20; i++){
            Page page = Page.getInstance(perSize,i);
            this.put(page);
        }
        this.logger.info("PageBlockingQueue inited");
    }

}
